package com.hr.rbac.dao.Impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.junit.Test;

import com.hr.rbac.dao.FunctionDao;
import com.hr.rbac.domain.Function;
import com.hr.rbac.domain.Privilege;
import com.hr.utils.TxQueryRunner;

/**
 * 权限对应功能实现层
 * @author 杨谦
 * @date 2015-11-5
 *
 */
public class FunctionDaoImpl implements FunctionDao{
	
	private QueryRunner qr = new TxQueryRunner();
	
	@Override
	public List<Function> findByPrivilegeID(String privilegeID) {
		// TODO Auto-generated method stub
		try {
			String sql = "select * from FUNCTION_PRIVILEGE where privilege_ID = ?";
			List<Object> functionID_List = qr.query(sql, new ColumnListHandler("function_ID"), privilegeID);
			
			List<Function> functions = new ArrayList<Function>();
			for(int i = 0;i < functionID_List.size();i++) {
				functions.add(findByfunctionID((String)functionID_List.get(i)));
			}
			return functions;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public List<Function> findByPrivilegeID(Privilege privilege) {
		// TODO Auto-generated method stub
		try {
			String sql = "select * from FUNCTION_PRIVILEGE where privilege_ID = ? and role_id = ?";
			List<Object> functionID_List = qr.query(sql, new ColumnListHandler("function_ID"), privilege.getPrivilege_id(),privilege.getRole_id());
			List<Function> functions = new ArrayList<Function>();
			for(int i = 0;i < functionID_List.size();i++) {
				functions.add(findByfunctionID((String)functionID_List.get(i),privilege.getRole_id()));
			}
			return functions;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	private Function findByfunctionID(String functionID, String role_id) {
		// TODO Auto-generated method stub
		try {
			String sql = "select * from HR_FUNCTION where function_ID = ?";
			Function function = qr.query(sql,new BeanHandler<Function>(Function.class),functionID);
			function.setRole_id(role_id);
			return function;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Test
	public void test_findByPrivilegeID(){
		System.out.println(findByPrivilegeID("dbb80172648a445d82579dbda5b74c93"));
	}

	@Override
	public Function findByfunctionID(String functionID) {
		// TODO Auto-generated method stub
		try {
			String sql = "select * from HR_FUNCTION where function_ID = ?";
			return qr.query(sql,new BeanHandler<Function>(Function.class),functionID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public Function findByfunctionIDAndPutInRoleID(String functionID) {
		// TODO Auto-generated method stub
		try {
			String sql = "select * from HR_FUNCTION where function_ID = ?";
			return qr.query(sql,new BeanHandler<Function>(Function.class),functionID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Function> findAllfunctionsByPrivilegeID(String privilege_id) {
		// TODO Auto-generated method stub
		try {
			String sql = "select * from TEMPLET_FUNCTION_PRIVILEGE where privilege_ID = ?";
			List<Object> functionID_List = qr.query(sql, new ColumnListHandler("function_ID"), privilege_id);
			List<Function> functions = new ArrayList<Function>();
			for(int i = 0;i < functionID_List.size();i++) {
				functions.add(findByfunctionID((String)functionID_List.get(i)));
			}
			return functions;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

}
